use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::models::entity::role;

/// 角色详情
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct RoleDetail {
    /// id
    pub id: i32,
    /// 名称
    pub name: String,
    /// 描述
    pub desc: Option<String>,
    /// 创建时间
    pub create_time: String,
    /// 更新时间
    pub update_time: String,
}

/// 角色列表
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct RoleList {
    /// id
    pub id: i32,
    /// 名称
    pub name: String,
    /// 描述
    pub desc: Option<String>,
    /// 创建时间
    pub create_time: String,
    /// 更新时间
    pub update_time: String,
}

impl From<role::Model> for RoleDetail {
    fn from(value: role::Model) -> Self {
        let fmt = "%Y-%m-%d %H:%M:%S";
        Self {
            id: value.id,
            name: value.name,
            desc: value.desc,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}

impl From<role::Model> for RoleList {
    fn from(value: role::Model) -> Self {
        let fmt = "%Y-%m-%d %H:%M:%S";
        Self {
            id: value.id,
            name: value.name,
            desc: value.desc,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}
